import express from 'express';
import cors from 'cors';
import mongoose from 'mongoose';
import { dbConnectMySQL } from './utilities';
import { TYPE_ENV, API_PORT, DB_URL_NOSQL, API_PATH } from './constants';
import {
	usersRouter,
	reportsRouter,
	additionalsRouter,
	ordersRouter_BN,
	ordersRouter_HB,
	ordersRouter_KX,
	ordersRouter_BZ,
	ordersRouter_GX,
	ordersRouter_BT,
	ordersRouter_PX,
	ordersRouter_MG,
	ordersRouter_KC,
	ordersRouter_GT,
	ordersRouter_BG,
	ordersRouter_BX,
	ordersRouter_CX,
	ordersRouter_BP,
	ordersRouter_TG,
	ordersRouter_XG,
	ordersRouter_CN,
	ordersRouter_BM,
	ordersRouter_AX,
	ordersRouter_CM,
	ordersRouter_XT,
} from './routes';

const app = express();

app.use(cors());
app.use(express.json());

if (TYPE_ENV === 'production') {
	// Express will serve up production assets
	app.use(express.static('client/build'));
	// console.log(`Docker with IP 192.168.99.100`);
}

app.use(`/${API_PATH}/users`, usersRouter);
app.use(`/${API_PATH}/reports`, reportsRouter);
app.use(`/${API_PATH}/additionals`, additionalsRouter);
app.use(`/${API_PATH}/orders-bn`, ordersRouter_BN);
app.use(`/${API_PATH}/orders-hb`, ordersRouter_HB);
app.use(`/${API_PATH}/orders-kx`, ordersRouter_KX);
app.use(`/${API_PATH}/orders-bz`, ordersRouter_BZ);
app.use(`/${API_PATH}/orders-gx`, ordersRouter_GX);
app.use(`/${API_PATH}/orders-bt`, ordersRouter_BT);
app.use(`/${API_PATH}/orders-px`, ordersRouter_PX);
app.use(`/${API_PATH}/orders-mg`, ordersRouter_MG);
app.use(`/${API_PATH}/orders-kc`, ordersRouter_KC);
app.use(`/${API_PATH}/orders-gt`, ordersRouter_GT);
app.use(`/${API_PATH}/orders-bg`, ordersRouter_BG);
app.use(`/${API_PATH}/orders-bx`, ordersRouter_BX);
app.use(`/${API_PATH}/orders-cx`, ordersRouter_CX);
app.use(`/${API_PATH}/orders-bp`, ordersRouter_BP);
app.use(`/${API_PATH}/orders-tg`, ordersRouter_TG);
app.use(`/${API_PATH}/orders-xg`, ordersRouter_XG);
app.use(`/${API_PATH}/orders-cn`, ordersRouter_CN);
app.use(`/${API_PATH}/orders-bm`, ordersRouter_BM);
app.use(`/${API_PATH}/orders-ax`, ordersRouter_AX);
app.use(`/${API_PATH}/orders-cm`, ordersRouter_CM);
app.use(`/${API_PATH}/orders-xt`, ordersRouter_XT);

// подключение mongoose к NOSQL_DB
mongoose
	.connect(DB_URL_NOSQL, {
		useNewUrlParser: true,
		useCreateIndex: true,
		useUnifiedTopology: true,
	})
	.then(() => console.log(`MongoDB connected successfully`))
	.catch((err) => console.log(`${err}: Could not connect to MongoDB...`));

// подключение mongoose к MySQL_DB_HOSTING
// dbConnectMySQL_HOSTING
dbConnectMySQL
	.sync({ force: false })
	.then(() => console.log(`MySQL connected successfully`))
	.catch((err) => console.log(`${err}: Could not connect to MySQL...`));

// подключение mongoose к MySQL_DB_LOCAL
// dbConnectMySQL_LOCAL
// 	.sync({ force: false })
// 	.then(() => console.log(`MySQL_DB_LOCAL connected successfully`))
// 	.catch((err) => console.log(`${err}: Could not connect to MySQL...`));

// запускаем сервер
app.listen(API_PORT, () => {
	console.log(`Server is running on port: ${API_PORT}`);
});
